package com.westcom.vulb.db;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.westcom.vulb.application.GlobalContext;
import com.westcom.vulb.bean.Session;

public class DbHelper extends OrmLiteSqliteOpenHelper 
{
    public static final String NAME = "yuelao.db";
    private static final int VERSION = 1;
    private Dao<Session, Integer> mSessionDao = null;
    private static DbHelper mInstance = null;
    
    public DbHelper(Context ctx) 
    {
        super(ctx, NAME, null, VERSION);
    }
    public static DbHelper getInstance()
    {
    	if(null == mInstance)
    	{
    		mInstance = new DbHelper(GlobalContext.getInstance().getContext());
    	}
    	return mInstance;
    }
    @Override
    public void onCreate(SQLiteDatabase database,ConnectionSource connectionSource) 
    {
        try 
        {
            TableUtils.createTableIfNotExists(connectionSource, Session.class);
        } catch (SQLException e) 
        {
            //TODO Database create error
            e.printStackTrace();
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase database,ConnectionSource connectionSource, int oldVersion, int newVersion) 
    {
        // do nothing
    }
    public Dao<Session, Integer> getSessionDao(Class c)
    {
    	if(null == mSessionDao)
    	{
    		try
    		{
    			mSessionDao = getDao(c);
    		}catch(SQLException e)
    		{
    			e.printStackTrace();
    		}
    	}
    	return mSessionDao;
    }
}
